#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use FindBin;
use lib ("$FindBin::Bin/.");
use die_error;
#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888
my $usage = <<"USAGE";

USAGE:

    non_tail.pl
            [--lines N]         keep all but the last N lines. Default = 10
			[--help]			print this message
			[--verbose]
            
USAGE

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory


# optional parameters
my $help = undef;
my $verbose;
my $cnt_lines = 1;
GetOptions(
			'verbose|v'	=> \$verbose,
			'help|h'	=> \$help,
			'lines|n=i'	=> \$cnt_lines);

die $usage if ($help);
die $usage unless ($cnt_lines > 0);

unshift (@ARGV,  '-') unless @ARGV;
print STDERR "\t[" if $verbose;
for my $file_name(@ARGV)
{
	print STDERR "." if $verbose;
	open_or_die(*FH, $file_name);
	my @lines;
	while (<FH>)
	{
		unshift(@lines, $_);
		if (@lines > $cnt_lines)
		{
			print pop(@lines);
		}
	}
}

print STDERR "]\nFinished\n\n" if $verbose;

